package com.lenovo.manufacture.module;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.beardedhen.androidbootstrap.BootstrapButton;
import com.beardedhen.androidbootstrap.BootstrapLabel;
import com.lenovo.manufacture.BaseActivity;
import com.lenovo.manufacture.R;
import com.lenovo.manufacture.annotation.Layout;

import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Module14 extends BaseActivity {
    List<Map<String, String>> materials;
    @BindView(R.id.rv_module14)
    RecyclerView rvModule14;
    @BindView(R.id.tv_gold)
    TextView tvGold;
    @BindView(R.id.tv_price)
    BootstrapLabel tvPrice;
    @BindView(R.id.tv_sum)
    BootstrapLabel tvSum;
    RvAdapter adapter;
    @BindView(R.id.rb_1)
    RadioButton rb1;
    @BindView(R.id.rb_2)
    RadioButton rb2;
    @BindView(R.id.rb_3)
    RadioButton rb3;
    @BindView(R.id.rb_4)
    RadioButton rb4;
    @BindView(R.id.rg_1)
    RadioGroup rg1;

    @Layout(value = R.layout.activity_module14, title = "购买原材料")
    protected void init() {
        async(apiService.getInfo(param.add("id", 1).build()), data -> {
            String gold = data.getData().get(0).get("gold");
            tvGold.setText("工厂资金：" + gold);
        });
        getMaterial();
    }

    private void getMaterial() {
        async(apiService.getMaterial(), data -> {
            materials = data.getData();
            adapter = new RvAdapter();
            rvModule14.setLayoutManager(new LinearLayoutManager(this));
            rvModule14.setAdapter(adapter);
        });
    }

    @OnClick({R.id.tv_price, R.id.tv_sum})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_price:
                sort(materials, "price", tvPrice, "价格");
                break;
            case R.id.tv_sum:
                sort(materials, "num", tvSum, "数量");
                break;
        }
        adapter.notifyDataSetChanged();
    }

    class RvAdapter extends RecyclerView.Adapter<ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_module14, parent, false);
            return new ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            Map<String, String> map = materials.get(position);
            holder.lv1.setText(map.get("materialName"));
            holder.lv2.setText(decimalFormat.format(Integer.parseInt(map.get("price"))));
            holder.lv3.setText(map.get("num"));
            holder.lv4.setText(map.get("supplyName"));
            holder.btnBuy.setOnClickListener(v -> {
                buy(map);
            });
        }

        @Override
        public int getItemCount() {
            return materials.size();
        }
    }

    private void buy(Map<String, String> material) {
        int pos = -1;
        switch (rg1.getCheckedRadioButtonId()) {
            case R.id.rb_1:
                pos = 0;
                break;
            case R.id.rb_2:
                pos = 1;
                break;
            case R.id.rb_3:
                pos = 2;
                break;
            case R.id.rb_4:
                pos = 3;
                break;
        }
        if (pos == -1) {
            showMsg("请选择车间");
        } else {
            async(apiService.userLineSearch(param.add("pos",pos).build()), data -> {
                Map<String, String> userLine = data.getData().get(0);
                async(apiService.addMaterial(param.add("userLineId", userLine.get("lineId"))
                        .add("supplyListId", material.get("supplyListId"))
                        .add("num", 1).build()), data1 -> {
                    showMsg("购买成功");
                    init();
                });
            });
        }
    }

    static
    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.lv_1)
        TextView lv1;
        @BindView(R.id.lv_2)
        TextView lv2;
        @BindView(R.id.lv_3)
        TextView lv3;
        @BindView(R.id.lv_4)
        TextView lv4;
        @BindView(R.id.btn_buy)
        BootstrapButton btnBuy;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}